package com.liaoyifan.core.filter;

import com.liaoyifan.core.util.RequestContext;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;

@SuppressWarnings("unused")
public class TraceContextFilter implements Filter {

    @Override
    public void doFilter(
            ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        try {
            RequestContext.init((HttpServletRequest) servletRequest);
            chain.doFilter(servletRequest, servletResponse);
        } finally {
            RequestContext.clear();
        }
    }
}
